<?php

App::uses('AppModel', 'Model');

/**
 * User Model
 *
 * @property UserGroup $UserGroup
 * @property Project $Project
 */
class User extends AppModel {

    /**
     * Validation rules
     *
     * @var array
     */
    public $validate = array(
        'username' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Your custom message here',
            ),
        ),
        'password' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Please enter password',
            ),
            'passwordLength' => array(
                'rule' => array('minLength', '6'),
                'message' => 'Use at least 6 characters.'
            ),
            'text' => array(
                'rule' => '/^[A-Za-z0-9@_]+$/i',
                'message' => 'Only letters and numeric [a-zA-Z0-9@_]'
            )
        ),
        'confirm_password' => array(
            'notempty' => array(
                'rule' => 'notEmpty',
                'required' => 'create',
                'allowEmpty' => false,
                'message' => 'Please enter comfirm password.'
            ),
            'passwordLength' => array(
                'rule' => array('minLength', '6'),
                'message' => 'Use at least 6 characters.'
            ),
            'text' => array(
                'rule' => '/^[A-Za-z0-9@_]+$/i',
                'message' => 'Only letters and numeric [a-zA-Z0-9@_]'
            ),
            'match' => array(
                'rule' => 'validatePasswdConfirm',
                'message' => 'Passwords do not match'
            )
        ),
        'first_name' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Please enter first name',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'last_name' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Please enter last name',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'email' => array(
            'email' => array(
                'rule' => array('email'),
                'message' => 'Please enter email address',
            ),
            'text' => array(
                //'rule' => '/^[A-Za-z0-9@_.]+$/i',
                'rule' => '/^((\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)\s*[.]{0,1}\s*)+$/i',
                'message' => 'Email format is incorrect.'
            ),
            'dupplicate' => array(
                'rule' => 'validateEmailDuplicate',
                'message' => 'Email is not available.'
            )
        ),
        'user_group_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    /**
     * belongsTo associations
     *
     * @var array
     */
    public $belongsTo = array(
        'UserGroup' => array(
            'className' => 'UserGroup',
            'foreignKey' => 'user_group_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    /**
     * hasMany associations
     *
     * @var array
     */
    public $hasMany = array(
        'Project' => array(
            'className' => 'Project',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    function validateEmailDuplicate($data) {
        if ($this->Find('first', array('conditions' => array('email' => $data['email'])))) {
            return false;
        }
        return true;
    }

    function validatePasswdConfirm($data) {
        if ($this->data['User']['password'] !== $data['confirm_password']) {
            return false;
        }
        return true;
    }
    
    public function val($email, $password) {
        $password = md5($password) . Configure::read('Security.salt');

        $chk_validate = $this->find('first', array(
            'conditions' => array(
                'User.email' => $email,
                'User.password' => $password
            )
        ));
        return $chk_validate;
    }

}
